package com.iwith.dongmoon.listadpater;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.iwith.dongmoon.activity.MemberDetailActivity;
import com.iwith.dongmoon.activity.R;
import com.iwith.dongmoon.gsonlist.MemberListItem;
import com.iwith.dongmoon.util.SessionManager;

public class FiveTextListAdapter extends ArrayAdapter<MemberListItem>{
	private Context mContext;
	private ArrayList<MemberListItem> items;
	private LayoutInflater mInflater;
	private SessionManager mSessionManager;
	
	public FiveTextListAdapter(Context context, int textViewResourceId,
			ArrayList<MemberListItem> list) {
		super(context, textViewResourceId, list);
		// TODO Auto-generated constructor stub

		this.mContext = context;
		this.items = list;
		this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mSessionManager = new SessionManager(mContext);
	}
	
	class ViewHolder {
		TextView bizname;		
		TextView graduation;
		TextView name;
		TextView bizphone;
		TextView distance;
	}
	
	@Override
	public MemberListItem getItem(int position) {
		if(items==null)		return null;
		else 				return items.get(position);
	}

	@Override
	public int getCount(){
		if(items==null)		return 0;
		else				return super.getCount();
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder=new ViewHolder();
			convertView = mInflater.inflate(R.layout.listitem_fivetext, null);
			holder.bizname = (TextView) convertView.findViewById(R.id.listitemfivetext_text1);
			holder.graduation = (TextView) convertView.findViewById(R.id.listitemfivetext_text2);
			holder.name=(TextView) convertView.findViewById(R.id.listitemfivetext_text3);
			holder.bizphone=(TextView) convertView.findViewById(R.id.listitemfivetext_text4);			
			holder.distance = (TextView) convertView.findViewById(R.id.listitemfivetext_text5);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final MemberListItem item = items.get(position);

		if (item != null) {
			holder.bizname.setText(item.getBizName());
			holder.graduation.setText(item.getGraduation().toString()+"기");
			holder.name.setText(item.getUsername());
			holder.bizphone.setText(item.getBizPhone());
			holder.distance.setText(String.format("%3.2f", item.getDistance())+"km");
			convertView.setBackgroundColor((position & 1) == 1 ? 0xffececec : Color.WHITE);
		}
		
		convertView.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, MemberDetailActivity.class);
				String favoriteid = item.getUserid();
				String userid = mSessionManager.getUserid();
				intent.putExtra("userid", userid);
				intent.putExtra("favoriteid", favoriteid);
				mContext.startActivity(intent);
			}
		});
		
		return convertView;
	}
}
